package com.sgb.zhaocai.viewmodel;

import android.text.TextUtils;

import androidx.lifecycle.ViewModel;

import com.sgb.zhaocai.base.BaseApi;
import com.sgb.zhaocai.entity.DemandIssueListEntity;
import com.sgb.zhaocai.entity.RequestBean;
import com.sgb.zhaocai.view.demand.fragment.DemandIssueFragment;
import com.swgk.core.base.api.ICallBack;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.base.model.entity.H5ReEntity;
import com.swgk.core.base.model.entity.ListResponse;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MToast;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Time:2021/9/28
 * AUTHOR:F_Q
 * Description:
 */
public class DemandIssueFragmentModel extends ViewModel {

    private boolean isLastPage;

    public void getBusinessPlanList(DemandIssueFragment fragment, RequestBean requestBean) {
        DialogHelper.showProgressDialog(fragment.getContext(), null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        BaseApi.getApi().getBusinessPlanList(requestBean).enqueue(new ICallBack<BaseEntity<ListResponse<List<DemandIssueListEntity>>>>() {
            @Override
            public void onSuccess(BaseEntity<ListResponse<List<DemandIssueListEntity>>> data) {
                DialogHelper.dismissProgressDialog();
                if (data.getState().equals(BaseApi.RESPONSE_OK)){
                    isLastPage = data.getData().getCurrentPage() >= data.getData().getTotal();
                    fragment.setBusinessPlanList(data.getData().getList(),isLastPage);
                }else {
                    MToast.showToast(fragment.getContext(),data.getMsg());
                }
            }
        });

    }

    public void getIsPublicity(DemandIssueFragment fragment, String announce, String id) {
        Map map = new HashMap();
        if (TextUtils.equals(announce,"1")){
            map.put("announce","0");
        }else {
            map.put("announce","1");
        }
        map.put("planId",id);
        DialogHelper.showProgressDialog(fragment.getContext(), null, "数据加载中", 0, true, null).setCanceledOnTouchOutside(false);
        BaseApi.getApi().getUpdateBusinessPlanIsAnnounce(map).enqueue(new ICallBack<BaseEntity>() {
            @Override
            public void onSuccess(BaseEntity data) {
                DialogHelper.dismissProgressDialog();
                if (data.getState().equals(BaseApi.RESPONSE_OK)){
                    fragment.setIsPublicity();
                }else {
                    MToast.showToast(fragment.getContext(),data.getMsg());
                }
            }
        });
    }

    public void getWebH5(DemandIssueFragment fragment, String id) {
        DialogHelper.showProgressDialog(fragment.getContext(), null, "数据加载中", 0, true, null).setCanceledOnTouchOutside(false);
        BaseApi.getApi().getUrlSetting("Demandetails").enqueue(new ICallBack<BaseEntity<H5ReEntity>>() {
            @Override
            public void onSuccess(BaseEntity<H5ReEntity> data) {
                DialogHelper.dismissProgressDialog();
                if (data.getState().equals(BaseApi.RESPONSE_OK)){
                    String webUrl = data.getData().getUrl() + "?id=" + id;
                    BaseApi.startWeb(fragment.getContext(), webUrl);
                }else {
                    MToast.showToast(fragment.getContext(),data.getMsg());
                }
            }
        });
    }
}
